#include "./../inc/game_map.h"

GameMap *GameMap::m_instance = NULL;

GameMap::~GameMap()
{
}

GameMap *GameMap::instance(int *mp, int difficulty)
{
    // 如果没有实例则创建实例
    if (m_instance == NULL)
    {
        switch (difficulty)
        {
        case 1:
            m_instance = new MediumGameMap(mp);
            break;
        case 2:
            m_instance = new HardGameMap(mp);
            break;
        default:
            m_instance = new EasyGameMap(mp);
            break; // 默认简单
        }
    }
    return m_instance;
}

void GameMap::delInstance()
{
    // 释放实例空间
    delete m_instance;
    m_instance = NULL;
}

void GameMap::init()
{
    // 绘制背景
    bg.show(mp, MAP_START_X);
    // 初始化网格数组
    for (int i = 0; i < GRID_ROWS; i++)
    {
        for (int j = 0; j < GRID_COLS; j++)
        {
            grid[i][j] = GRID_EMPTY;
        }
    }
    food_count = 0;
    grid[0][0] = GRID_SNAKE_BODY; // 初始化蛇身
}

// 绘制果实
void GameMap::drawFood(int row, int col, GridState type)
{
    int pixelX = MAP_START_X + col * GRID_SIZE;
    int pixelY = row * GRID_SIZE;
    // 根据果实类型绘制对应图片
    switch (type)
    {
    case GRID_FOOD_SCORE_1:
        food_score1.show(mp, pixelX, pixelY);
        break;
    case GRID_FOOD_SCORE_2:
        food_score2.show(mp, pixelX, pixelY);
        break;
    case GRID_FOOD_SPEED_UP:
        food_speedup.show(mp, pixelX, pixelY);
        break;
    case GRID_FOOD_SPEED_DOWN:
        food_speeddown.show(mp, pixelX, pixelY);
        break;
    case GRID_FOOD_CONFUSE:
        food_confuse.show(mp, pixelX, pixelY);
        break;
    case GRID_FOOD_BOMB:
        food_bomb.show(mp, pixelX, pixelY);
        break;
    case GRID_FOOD_AI_SNAKE:
        food_ai_snake.show(mp, pixelX, pixelY);
    default:
        break;
    }
}

// 新增：随机生成果实（含绘制）
void GameMap::generateRandomFood()
{

    if (food_count >= max_food_count)
        return;
    int randRow, randCol;
    int maxRetry = 1000; // 最大重试次数（防止蛇占满网格时卡死）
    int retryCount = 0;

    // 找空白格子（避开蛇身/已有食物），添加重试限制
    do
    {
        randRow = rand() % GRID_ROWS;
        randCol = rand() % GRID_COLS;
        retryCount++;
        if (retryCount >= maxRetry)
        {
            // 重试1000次仍未找到空白格，说明蛇快占满屏幕，可返回（或触发游戏胜利）
            std::cout << "网格剩余空白太少，无法生成食物！" << std::endl;
            return;
        }
    } while (grid[randRow][randCol] != GRID_EMPTY);

    // 随机选果实类型
    int randType = rand() % 100;
    GridState foodType;
    int s1 = getScore1Prob();
    int s2 = getScore2Prob();
    int su = getSpeedUpProb();
    int sd = getSpeedDownProb();
    int c = getConfuseProb();
    int b = getBombProb();

    if (randType < s1)
        foodType = GRID_FOOD_SCORE_1;
    else if (randType < s1 + s2)
        foodType = GRID_FOOD_SCORE_2;
    else if (randType < s1 + s2 + su)
        foodType = GRID_FOOD_SPEED_UP;
    else if (randType < s1 + s2 + su + sd)
        foodType = GRID_FOOD_SPEED_DOWN;
    else if (randType < s1 + s2 + su + sd + c)
        foodType = GRID_FOOD_CONFUSE;
    else if (randType < s1 + s2 + su + sd + c + b)
        foodType = GRID_FOOD_BOMB;
    else
        foodType = GRID_FOOD_AI_SNAKE;

    grid[randRow][randCol] = foodType;
    food_count++;
    drawFood(randRow, randCol, foodType);
}

void GameMap::drawMap()
{
    // 绘制背景
    bg.show(mp, MAP_START_X);
    // 绘制果实
    for (int i = 0; i < GRID_ROWS; i++)
    {
        for (int j = 0; j < GRID_COLS; j++)
        {
            drawFood(i, j, grid[i][j]);
        }
    }
}